package com.utils.string;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * @description String[]转换bean工具类
 */
public class TransBeanUtils<E> {

    public static <T> T getMappingPo(Class<T> classz, Object[] objs) throws Exception {
        Field[] fields = classz.getDeclaredFields();
        Object object = classz.newInstance();
        int length = objs.length;

        for (Field fild : fields) {
            String typeStr = fild.getType().getSimpleName().toLowerCase();

            Annotation[] annos = fild.getAnnotations();

            if (annos.length > 0) {
                for (Annotation anno : annos) {
//                    if (anno instanceof  BOIndex){
//                        System.out.println(anno.annotationType().getSimpleName());
//                        System.out.println("------------------------");
//                    }


                    if (anno instanceof BOIndex) {
                        BOIndex myAmoncation = (BOIndex) anno;
                        int index = myAmoncation.index();

                        if (index < length) {
                            fild.setAccessible(true);


                            if ("double".equals(typeStr)) {
                                fild.set(object, Double.valueOf(objs[index] == null ? "0" : objs[index].toString()));
                            }

                            else if ("int".equals(typeStr) || "integer".equals(typeStr)) {
                                fild.set(object, Integer.valueOf(objs[index] == null ? "0" : objs[index].toString()));
                            }

                            else if ("float".equals(typeStr)) {
                                fild.set(object, Float.valueOf(objs[index] == null ? "0" : objs[index].toString()));
                            }

                            else if ("long".equals(typeStr)) {
                                fild.set(object, Long.valueOf(objs[index] == null ? "0" : objs[index].toString()));
                            }

                            else if ("date".equals(typeStr)) {
                                System.out.println("时间数据: "+objs[index].toString());
                                //fild.set(object, SGS.asDate(objs[index] == null ? null : objs[index].toString()));
                            }
                            else {

                                fild.set(object, objs[index]);
                            }
                        }
                    }
                }
            }
        }
        return (T) object;

    }

    /**
     * @description 解析Txt文件
     */
    public static <T> List<T> resolverTxt(String tmpPath, Class<T> clazz, String splitSign) throws Exception {
        File file = new File(tmpPath);
        boolean exists = file.exists();
        if (!exists) {
            throw new Exception("文件不存在");
        }
        //利用输入流获取TXT文件内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8));

        List<T> beans = new ArrayList<>();

        String tempString;
        int line = 1;
        while ((tempString = reader.readLine()) != null) {
            if (StringUtils.isBlank(tempString)){
                continue;
            }
            System.out.println("Line" + line + ":" + tempString);
            String[] split = tempString.split(splitSign);
            T bean = TransBeanUtils.getMappingPo(clazz, split);
            beans.add(bean);
            line++;
        }
        return beans;
    }

}